<?php include dirname(__FILE__).'/common/header.php'; ?>

<?php include dirname(__FILE__).'/common/menu.php'; ?>

var attr = {};

	attr.project_list = <?php echo $attr['project_list'] ?>;
	attr.project_id = Ext.util.Cookies.get('project_id')||'';


	list.grid = new ZM.GridPanel({
		url: list.baseUrl + 'template/getTemplateList',
		history:'template_id',
		onlyShow: true,
		region:'center',
		tbar:[
			{
				text:'创建模板',
				cls:'btn-show',
				iconCls:'icon-create',
				handler:function(){
					var frm = new ZM.FormPanel({
						url : list.baseUrl + 'template/createTemplate',
						keySubmit:true,
						success:function(res) {
							ZM.bo.msg('success',res.detail);
							attr.project_id = res.addon.project_id;
							Ext.util.Cookies.set('project_id',res.addon.project_id);

							list.grid.store.reload();
							win.close();
						},
						items:[
							{
								fieldLabel:'模板名称',
								name:'template_name',
								allowBlank:false
								
							},
							{	
								xtype:'combofield',
								fieldLabel:'项目名称',
								name:'project_id',
								value:attr.project_id||'',
								options:attr.project_list,
								allowBlank:false
							},
							{	
								fieldLabel:'模板标题',
								name:'template_title'
							}
						]
					});
					var win = new ZM.PopUp({
						title:'创建模板',
						type:'s',
						items:frm
					});
					win.show();
				}
			}
		],
		//group:{field:'project_id',groupView:true,tpl:'{group}'},
		columns:[
			{header:'ID',dataIndex:'template_id',filter:{},sort:true,width:80},
			{header:'模板名称',dataIndex:'template_name',width:150,filter:{}},
			{header:'模板标题',dataIndex:'template_title',width:150,filter:{},toggle:true},
			{header:'Widgets',dataIndex:'template_struct_plain',width:150,filter:{name:'template_struct'}},
			{header:'项目名称',dataIndex:'project_id',width:120
				,renderer:function(val) {
					return ZM.id2name(val,attr.project_list);
				},filter:{
				xtype:"combofield",
				listWidth:180,
				options:[['','全部']].concat(attr.project_list)
			}},
			{header:'最后更改用户',dataIndex:'user_name',width:90},
			{header:'最后修改时间',dataIndex:'template_time_lastmodified',renderer:ZM.diffDay,sort:true,width:140},
			{header:'操作',dataIndex:'',renderer:function(val,m,r) {
					var str = '';
					str += Ext.rd('编辑代码','fireEditTemplate',r,'template/editTemplate');
					str += Ext.rd('复制','fireCopyTemplate',r,'template/cloneTemplate');
					str += Ext.rd('编辑基础信息','fireEditTemplateBasic',r,'template/editTemplateBasicInfo');
					str += Ext.rd('模块列表','fireViewWidget',r,'template/getTemplateWidgetList');
					str += '<a href="/t/'+r.json.template_id+'/1" target="_blank">[预览]</a>';
					//str += Ext.rd('下载CSS','fireDownloadCss',r,'template/downloadTemplateCss');
					str += Ext.rd('生成静态页面','fireDownloadPage',r,'template/downloadStaticPage');
					str += Ext.rd('无图片静态页面','fireNoImageDownloadPage',r,'template/downloadNoImageStaticPage');
					str += Ext.rd('删除','fireDeleteTemplate',r,'template/deleteTemplate');
					return str;
				}
			}
		]
	});

	list.grid.fireDownloadCss = function(r) {
		var r = r.json;
		Ext.Msg.confirm('确认?','确定要导出['+r.template_name+']的样式文件?',function(btn) {
			if(btn != 'no') {
				location.href = list.baseUrl + 'template/downloadTemplateCss/'+r.template_id;
			}
		});
	}

	list.grid.fireDownloadPage = function(r) {
		var r = r.json;
		Ext.Msg.confirm('确认?','确定要生成['+r.template_name+']页面?',function(btn) {
			if(btn != 'no') {
				location.href = list.baseUrl + 'template/downloadStaticPage/'+r.template_id;
			}
		});
	}

	list.grid.fireNoImageDownloadPage = function(r) {
		var r = r.json;
		Ext.Msg.confirm('确认?','确定要生成不含图片的静态['+r.template_name+']页面?',function(btn) {
			if(btn != 'no') {
				location.href = list.baseUrl + 'template/downloadNoImageStaticPage/'+r.template_id;
			}
		});
	}
	
	list.grid.fireCopyTemplate = function(r) {
		var r = r.json;
		var frm = new ZM.FormPanel({
			url: list.baseUrl + 'template/cloneTemplate',
			params:{template_id:r.template_id},
			success:function(res) {
				ZM.bo.msg('成功!',res.detail);
				win.close();
				list.grid.store.reload();
			},
			items:[{
				fieldLabel:'名称',
				name:'template_name',
				value: r.template_name+'(copy)',
				allowBlank:false
			}]
		});
		var win = new ZM.PopUp({
			type:'s',
			title: '复制模版['+r.template_name+']',
			items:frm
		});
		win.show();
	}
	
	list.grid.fireEditTemplateBasic = function(r) {
		var r = r.json;
		var frm = new ZM.FormPanel({
			url:list.baseUrl + 'template/editTemplateBasicInfo',
			keySubmit:false,
			params:{template_id:r.template_id},
			success:function(res){
				ZM.bo.msg('成功!',res.detail);
				win.close();
				list.grid.store.reload();
			},
			items:[{
				fieldLabel:'模板名称',
				xtype : 'textfield',
				name:'template_name',
				value: r.template_name,
				allowBlank:false
			},{
				xtype:'combofield',
				fieldLabel:'项目名称',
				name:'project_id',
				value: r.project_id,
				options: attr.project_list,
				allowBlank:false
			}, {	
				fieldLabel:'模板标题',
				name:'template_title',
				value: r.template_title
			}]
		});		
		var win = new ZM.PopUp({
			title:' 编辑模板['+r.template_name+']基础信息',
			type:'s',
			items:frm
		});
		win.show();
	}
	
	list.grid.fireEditTemplate = function(r) {
		var r = r.json;
		var frm = new ZM.FormPanel({
			tbar:[
				{
					text:'保存',
					iconCls:'icon-save',
					hidden:!Ext.permission['template/editTemplate'.toLowerCase()],
					cls:'btn-show',
					handler:function(){
						ZM.submitForm(frm);
					}
				}
			],
			region:'center',
			url:list.baseUrl + 'template/editTemplate',
			keySubmit:false,
			focusField: false,
			buttons:null,
			params:{template_id:r.template_id},
			success:function(res){
				ZM.bo.msg('操作成功',res.detail);
				list.grid.store.reload();
				frm.params.template_time_lastmodified = res.addon.template_time_lastmodified;
			},
			defaults:{width:650},	
			items:[{
				fieldLabel:'CSS',
				name:'template_css',
				height: 150,
				xtype:'textarea',
				id : 'csscreate',
				plugins:[new ZM.tabKey()]
			},{
				fieldLabel:'HTML',
				name:'template_struct',
				id:'field_template_struct',
				height: 250,
				xtype:'textarea',
				plugins:[new ZM.tabKey()],
				allowBlank:false,
				enableKeyEvents: true,
				listeners:{
					'afterrender':function(f) {
						f.el.on('mousedown',function(e,t){
							if(strWidget!='') {
								var me = this;
								setTimeout(function() {
										ZM.insertAtCursor(me.dom, strWidget);
										strWidget = '';
									},50);
							}
						},f.el);
					}
				}
			}]
		});		
		var strWidget = '';
		var wGrid = new ZM.GridPanel({
			url: list.baseUrl + 'template/getProjectWidgetList',
			params:{project_id:r.project_id},
			onlyShow:true,
			region:'east',
			width:130,
			collapsible:true,
			titleCollapse: true,
			remoteFilter:false,
			bbar:null,
			columns:[
				{header:'ID',dataIndex:'widget_id',hidden:true},
				{header:'模块名称',dataIndex:'widget_name',width:120,filter:{},renderer:function(val,m,r) {
					var str = '';
					str += '<a href="#" title="fireInsertWidget" id="_'+r.id+'" style="color:#111;text-decoration:none;display:block;width:100%;">'+val+'</a>';
					return str;
				}}
			],
			fireInsertWidget:function(r) {
				ZM.insertAtCursor(Ext.get('field_template_struct').dom,'\n<widget>'+r.json.widget_name+'</widget>');
			}
		});
		var win = new ZM.PopUp({
			title:' 编辑模板['+r.template_name+']代码',
			type:'l',
			width:1030,
			layout:'border',
			items: [frm,wGrid]
		});
		win.show();
		wGrid.store.load();
		frm.getEl().mask('数据获取中...','x-mask-loading');
		ZM.ajax({
			url:list.baseUrl + 'template/getTemplate',
			params:{template_id:r.template_id},
			success:function(res) {
				frm.getForm().findField('template_css').setValue(res.addon.template.template_css);
				frm.getForm().findField('template_struct').setValue(res.addon.template.template_struct);
				frm.params.template_time_lastmodified = res.addon.template.template_time_lastmodified;
				frm.getEl().unmask();
			},
			failure:function(res) {
				Ext.Msg.alert('失败','获取数据失败!点击确定关闭窗口!',function(){
					win.close();
				});
			}
		});
	}
	
	list.grid.fireDeleteTemplate = function(r) {
		var r = r.json;
		Ext.Msg.confirm('确认?','确定要删除模板['+r.template_name+']?',function(btn){
			if(btn == 'yes') {
				list.grid.getEl().mask('正在删除中...','x-mask-loading');
				ZM.ajax({
					url: list.baseUrl + 'template/deleteTemplate',
					params: {template_id:r.template_id},
					success:function(res){
						list.grid.getEl().unmask();
						ZM.bo.msg('success',res.detail);
						list.grid.store.reload();
					},
					failure:function(res) {
						list.grid.getEl().unmask();
					}
				});
			}
		});
	}
	
	list.grid.fireDetail = list.grid.fireViewWidget = function(r) {
		if(!Ext.permission['template/getTemplateWidgetList'.toLowerCase()]) return;
		attr.r = r.json;
		tab.setActiveTab(1);
		widgetGrid.store.baseParams = {template_id:attr.r.template_id};
		widgetGrid.store.load();
		list.detail.show().ownerCt.doLayout();

		
	}
	
	var widgetGrid = new ZM.GridPanel({
		url: list.baseUrl + 'template/getTemplateWidgetList',
		history:'widget_id',
		onlyShow: true,
		columns:[
			{header:'ID',dataIndex:'widget_id',hidden:true},
			{header:'模块名称',dataIndex:'widget_name',width:180},
			{header:'最后修改时间',dataIndex:'widget_time_lastmodified',renderer:ZM.diffDay,width:160},
			{header:'操作',dataIndex:'',renderer:function(val,m,r) {
				var str = '';
				str += '<a href="#" id="_'+r.id+'" title="fireSource">[HTML代码]</a>';
				str += '<a href="/widget_edit/'+r.json.widget_id+'" target="_blank">[编辑内容]</a>';
				
				return str;
			}}
		],
		bbar:null
	});

	widgetGrid.fireSource = function(r){
		var r = r.json;

		var eof = r.widget_html.match(/<![^>]*>/g)[1];

		var v = r.widget_html.replace(eof,'');
		if(r.widget_js!=''){
			v += '<script type="text/javascript">\n'+r.widget_js+'\n<\/script>\n';
		}
		v += eof;


		v = hljs.highlight('html', v).value;
		
		var win = new ZM.PopUp({
			title:'编辑模块['+r.widget_name+']',
			type:'l',
			items:{xtype:'panel',autoScroll: true,html:'<pre>'+v+'</pre>'}
		});
		win.show();
	}
	
	var tab = new Ext.TabPanel({
			defaults:{
				layout:'fit'
			},
		    items: [{
		        title: 'HTML 代码',
		        id:'teplate_code',
		        html: 'A simple tab',
		        autoScroll:true,
		        fn:function(){
		        	var v = attr.r.template_struct;
		        	this.update('<pre>'+hljs.highlight('html', attr.r.template_struct).value+'</pre>');
		
		        }

		    },{
		    	title:'模块列表',
		    	items:widgetGrid,
		    	fn:function(){
		    		//
		    	}
			},{
				title:'压缩css',
				xtype:'container',
				id:'template_css_code',
				autoScroll:true,
				html:'',
				listeners: {
					render:function(panel) {
						
					}
				},
				fn:function(){
		    		//
		    		var panel = this;
		    		ZM.ajax({
							url: list.baseUrl + 'template/viewStaticTemplateCss',
							params: {template_id:attr.r.template_id},
							mask:panel,
							success:function(res){
								panel.el.update(res.addon.css);
							},
							failure:function(res) {
							}
						});
		    	}
			}],
			listeners:{
			tabchange:function(ot,nt) {
					nt.fn();
				}
			}
		})
	list.detail = new ZM.detailPanel({
		items:tab
	});

<?php include dirname(__FILE__).'/common/layout.php'; ?>
list.grid.store.load();
<?php include dirname(__FILE__).'/common/footer.php'; ?>
